package txtpack.Work;

import java.io.*;
import java.nio.charset.Charset;

public class Exam03 {
    public static void main(String[] args) {
        //将一个UTF-8编码的文件转换成一个GBK格式的文件，分别使用字节流和字符流实现。
        File target  = new File("E:\\IO流练习\\InoutStream\\utf8.txt");
        File dest = new File("E:\\IO流练习\\字符流\\FileWrite\\CharGBK.txt");
        getGBK(target,dest);
    }
    private static void getGBK(File target , File dest){
        try (FileReader reader = new FileReader(target,Charset.forName("utf8"));
             FileWriter writer = new FileWriter(dest,Charset.forName("GBK"));
        ){
            char[] chars = new char[1024];
            int line;
            while ((line =reader.read(chars)) != -1) {
                writer.write(chars,0,line);
            }
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }
    private static void getGBK2(File target , File dest){
        try (FileInputStream in = new FileInputStream(target);
             InputStreamReader input = new InputStreamReader(in,Charset.forName("utf8"));
             FileOutputStream out = new FileOutputStream(dest);
             OutputStreamWriter output = new OutputStreamWriter(out,Charset.forName("GBK"));
        ){
            int size;
            while ((size = input.read()) != -1){
                output.write(size);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
